package com.nuoniu.sibanyun.mapper.crm;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.annotation.DataAuth;
import com.nuoniu.sibanyun.entity.SysArea;
import com.nuoniu.sibanyun.entity.crm.CrmCustomer;
import com.nuoniu.sibanyun.entity.crm.dto.SimpleCrmCustomerDto;
import com.nuoniu.sibanyun.entity.dto.*;
import com.nuoniu.sibanyun.entity.vo.CrmCustomerFinanceVo;
import com.nuoniu.sibanyun.entity.vo.SelectVo;
import com.nuoniu.sibanyun.entity.vo.SimpleCrmCustomerVo;
import com.nuoniu.sibanyun.mapper.baseMapper.CrmDynamicConditionMapper;
import com.nuoniu.sibanyun.mapper.provider.CrmDynamicConditionProvider;
import com.nuoniu.sibanyun.userThread.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 客户基本信息 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-15
 */
@Component
@Mapper
public interface CrmCustomerMapper extends BaseMapper<CrmCustomer>, CrmDynamicConditionMapper {

    /**
     *
     * @param page
     * @param crmCustomerSearchDto
     * @return
     */
    @DataAuth(tableAlias = "crm",personalField = "collar_user_id",companyField = "company_id",officeField = "office_id")
    Page<SelectVo> pageForSelect(Page page, @Param("dto") CrmCustomerSearchDto crmCustomerSearchDto,String companyTypeSql);

    Page<SimpleCrmCustomerVo> pageForVoucher(Page page,Integer companyId,@Param("dto") CrmCustomerSearchDto crmCustomerSearchDto,String companyTypeSql);

    @Select("select crm.* from pub_customer crm WHERE crm.id = #{id} AND crm.`status` = 0")
    @DataAuth(tableAlias = "crm",personalField = "collar_user_id",companyField = "company_id",officeField = "office_id")
    CrmCustomer findById(Integer id);


    @Select("select crm.`status` from pub_customer crm WHERE crm.id = #{id}")
    Integer getCrmCustomerStatus(Integer id);
    /**
     * 根据名称获取客户
     * @author 小啊刚
     * @date 2020/6/19
     */
    @Select("select * from pub_customer where crm_name = #{crmName} and company_id = #{companyId} and status!=1 and status!=2")
    CrmCustomer findByName(Integer companyId, String crmName);

    /**
     * 获取最新一条数据
     * @author 小啊刚
     * @date 2020/6/19
     */
    @Select("select * from pub_customer where company_id = #{companyId} and date(create_date) = #{createDate} order by create_date desc limit 1")
    CrmCustomer getFirstCustomer(Integer companyId, String createDate);

    /**
     * 修改状态
     * @author 小啊刚
     * @date 2020/6/19
     */
    @Update("update pub_customer set status = #{status} where id in (${ids})  and is_customer = 1")
    void updateStatus(String ids, Integer status);

    /**
     * 获取领用客户数量
     * @author 小啊刚
     * @date 2020/6/20
     */
    @Select("select count(1) from pub_customer where collar_user_id = #{userId} and status = 0 and crm_type = 1  and is_customer = 1")
    int getReceiveCount(Integer userId);

    /**
     * 获取用户客户数量
     * @author 小啊刚
     * @date 2020/6/20
     */
    @Select("select count(1) from pub_customer where collar_user_id = #{userId} and status = 0 and is_customer = 1")
    int getUserCount(Integer userId);

    @Select("select c.*, l.name linkmanName, l.gender linkmanGender, l.dept linkmanDept, l.position linkmanPosition, l.office_phone linkmanOfficePhone," +
            " l.mobile linkmanMobile, l.email linkmanEmail from pub_customer c left join crm_linkman l on c.id = l.crm_id where c.id in (${ids})")
    List<CrmExcelDto> getCrmList(String[] ids);

    /**
     * 批量插入 客户
     * @author 小啊刚
     * @date 2020/7/1
     */
    void insertList(List<CrmCustomer> crmCustomerList);

    /**
     * 批量释放客户
     * @param crmCustomerList
     */
    void batchReleaseCustomer(List<CrmCustomer> crmCustomerList);

    /**
     * 获取该租下所以客户名称
     * @author 小啊刚
     * @date 2020/7/2
     */
    @Select("select crm_name from pub_customer where company_id = #{companyId} and status != 1 and status != 2 ")
    List<String> getByCompanyId(Integer companyId);

    /**
     * 根据多个id查询客户列表
     * @author 小啊刚
     * @date 2020/7/2
     */
    @Select("select * from pub_customer where id in (${ids}) order by create_date DESC")
    List<CrmExcelDto> getCustomerList(String ids);

    @SelectProvider(type = CrmDynamicConditionProvider.class, method = "crmDynamicConditionSql")
    @DataAuth(tableAlias = "crm",personalField = "collar_user_id",companyField = "company_id",officeField = "office_id")
    Page<CrmExcelDto> crmListPage(DynamicDto dynamicDto,HashMap<String,Object> paramMap, Page page);

    /**
     * 修改客户编码
     * @author 小啊刚
     * @date 2020/7/6
     */
    @Update("update pub_customer set crm_no = REPLACE(crm_no, left(crm_no, length(crm_no) - 12), #{currentCode}) where company_id=#{companyId} and is_customer = 1")
    void updateCode(String currentCode, Integer companyId);

    /**
     * 获取潜在客户列表
     * @author 小啊刚
     * @date 2020-07-16
     */
    @Select("select * from pub_customer where crm_type = #{crmType} and company_id = #{companyId} and status = 0 and is_release = 1 and is_customer = 1")
    List<CrmCustomer> getListByType(Integer crmType, Integer companyId);

    /**
     * 修改创建人
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update ${tableName} set create_user_name = #{realName} where create_user_id = #{id}")
    void updateCreateName(String tableName, Long id, String realName);

    /**
     * 修改修改人
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update ${tableName} set update_user_name = #{realName} where update_user_id = #{id}")
    void updateUpdateName(String tableName, Long id, String realName);

    /**
     * 修改所属人
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update pub_customer set collar_user_name = #{realName} where collar_user_id = #{id} and is_customer = 1")
    void updateCollarName(Long id, String realName);

    /**
     * 上次领用人
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update pub_customer set last_collar_user_name = #{realName} where last_collar_user_id = #{id} and is_customer = 1")
    void updateLastCollarUserName(Long id, String realName);

    /**
     * 修改释放人
     * @author 小啊刚
     * @date 2020-08-05
     */
    @Update("update pub_customer set release_user_name = #{realName} where release_user_id = #{id} and is_customer = 1")
    void releaseUserName(Long id, String realName);

    /**
     * 客户跟进分析
     * @author 小啊刚
     * @date 2020-08-07
     */
    Page<Map> followAnalysis(Page page, @Param("analysis") CrmFollowAnalysis analysis, @Param("userInfo") UserInfo userInfo);

    /**
     * 客户分析table数据
     * @author 小啊刚
     * @date 2020-08-08
     */
    Map crmAnalysisTable(@Param("analysis") CrmFollowAnalysis analysis, @Param("userInfo") UserInfo userInfo);

    /**
     * 修改地区名称
     * @author 小啊刚
     * @date 2020-08-11
     */
    @Update("update pub_customer set area_name = #{treeNames},area_tree_codes = CONCAT(#{parentCodes},#{areaCode}) where area_id = #{id}")
    void updateAreaName(SysArea area);

    /**
     * 分页查询 财务 不可用/可用 的客户
     * @param page
     * @param crmCustomerFinanceDto
     * @return
     */
    Page<CrmCustomerFinanceVo> pageCrmCustomerFinanceVo(Page page, @Param("dto") CrmCustomerFinanceDto crmCustomerFinanceDto);

    @Select("select crm_name, industry, area_name, legal_person, enterprise_scale, register_capital,  remarks" +
            " from pub_customer where company_id=#{companyId} and is_finance=1 and is_customer = 1")
    List<HashMap<String, Object>> getErpList(Integer companyId);

    @Select("select crm_no from pub_customer where company_id = #{companyId} and date_format(create_date, #{dateStr})=date_format(now(), #{dateStr}) order by create_date desc limit 1")
    String getNewestCode(Integer companyId, String dateStr);

    /**
     * 获取领用后第一次联系时间
     * @author 小啊刚
     * @date 2022-01-17
     */
    @Select("select  contact_date from crm_touch where crm_id=#{id} and contact_date >= date(#{collarTime}) order by contact_date limit 1")
    LocalDateTime getFirstTouchDate(Long id, LocalDateTime collarTime);

    /**
     *  查询 还处于 外呼联系人 的客户
     * @param companyId
     * @param crmIds
     * @param taskId
     */
    Set<Long> listCrmIdFromTaskIngLinkMan(Integer companyId, Set<Long> crmIds, Integer taskId);
}
